<?php
/**
 * 自定义默认函数库
 */


/**
 * 根据模板返回表单验证的单条错误信息
 * @param $errors 表单验证信息对象
 * @param $name 当前验证字段
 * @return string
 */
function getErrorMsg($errors, $name)
{
    return $errors->has($name) ? $errors->first($name, '<label id="bv_required-error" class="error" for="bv_required" style="display: inline-block;">:message</label>') : '';
}

/**
 * 页面跳转提示
 * @param string $type  success(成功)/warning-(警告)/error-(错误)
 * @param string $msg   跳转描述语句
 * @param null $url     跳转地址
 * @param int $seconds  停留几秒跳转
 * @return $this
 */
function ShowMsg($type='success',$msg='',$url=null,$seconds=3)
{
    if (empty($url))
        $url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/';
    switch($type)
    {
        case 'success':
            $title = 'success';
            break;
        case 'warning':
            $title = 'warning';
            break;
        case 'error':
            $title = 'error';
            break;
        default:
            $title = 'success';
            break;
    }
    $jump = 'admin.prompt.jump-'.$title;
    $title = Lang::has('Tips.'.$title) ? Lang::get('Tips.'.$title) : $title;
    return view($jump)->with(['jumpmsg'=>$msg,'jumpurl'=>$url,'jumpseconds'=>$seconds,'title'=>$title]);
}

/**
 * 根据分组ID和分组类型获取分组名称
 * @param $id
 * @param string $type
 * @param string $key
 * @return string
 */
function getMemberGroup($id, $type = 'system', $key = 'name')
{
    switch ($type)
    {
        case 'system':
            $group = \App\Models\AdminGroup::find($id);
            break;
        case 'member':
            $group = \App\Models\MemberGroup::find($id);
            break;
        default:
            $group = null;
            break;
    }
    if($group)
    {
        return $group->$key;
    }
    return $id;
}


/**
 * 根据UID获取昵称或用户名
 * @param $uid
 * @return string
 */
function getUserName($uid)
{
    $user = \App\Models\User::find($uid);
    if ($user)
    {
        return $user->realName ? $user->realName : $user->name;
    }
    return 'NONE';
}
/** Json数据格式化
 * @param  array  $data   数据
 * @param  String $indent 缩进字符，默认4个空格
 * @return JSON
 */
function jsonFormat($data, $indent=null){
    // json encode
    $data = json_encode($data,JSON_UNESCAPED_UNICODE);

    // 缩进处理
    $ret = '';
    $pos = 0;
    $length = strlen($data);
    $indent = isset($indent)? $indent : '    ';
    $newline = "\n";
    $prevchar = '';
    $outofquotes = true;

    for($i=0; $i<=$length; $i++){

        $char = substr($data, $i, 1);

        if($char=='"' && $prevchar!='\\'){
            $outofquotes = !$outofquotes;
        }elseif(($char=='}' || $char==']') && $outofquotes){
            $ret .= $newline;
            $pos --;
            for($j=0; $j<$pos; $j++){
                $ret .= $indent;
            }
        }

        $ret .= $char;

        if(($char==',' || $char=='{' || $char=='[') && $outofquotes){
            $ret .= $newline;
            if($char=='{' || $char=='['){
                $pos ++;
            }

            for($j=0; $j<$pos; $j++){
                $ret .= $indent;
            }
        }

        $prevchar = $char;
    }

    return $ret;
}

function getLang($path, $key)
{
    return Lang::has($path.'.'.$key) ? Lang::get($path.'.'.$key) : $key;
}

/*手机号码验证*/
function checkMobile($mobilephone=''){
    if(strlen($mobilephone)!=11){	return false;	}
    if(preg_match("/^1[34578]\d{9}$/", $mobilephone)){
        return true;
    }else{
        return false;
    }
}

/*加密解密 ENCODE 加密   DECODE 解密*/
function _encrypt($string, $operation = 'ENCODE', $key = '', $expiry = 0){
    if($operation == 'DECODE') {
        $string =  str_replace('_', '/', $string);
    }
    $key_length = 4;
    $key = md5($key != '' ? $key : 'DD49C302A52DF385E874A51AA5542939ED6B3A93C737');
    $fixedkey = md5($key);
    $egiskeys = md5(substr($fixedkey, 16, 16));
    $runtokey = $key_length ? ($operation == 'ENCODE' ? substr(md5(microtime(true)), -$key_length) : substr($string, 0, $key_length)) : '';
    $keys = md5(substr($runtokey, 0, 16) . substr($fixedkey, 0, 16) . substr($runtokey, 16) . substr($fixedkey, 16));
    $string = $operation == 'ENCODE' ? sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$egiskeys), 0, 16) . $string : base64_decode(substr($string, $key_length));

    $i = 0; $result = '';
    $string_length = strlen($string);
    for ($i = 0; $i < $string_length; $i++){
        $result .= chr(ord($string{$i}) ^ ord($keys{$i % 32}));
    }
    if($operation == 'ENCODE') {
        $retstrs =  str_replace('=', '', base64_encode($result));
        $retstrs =  str_replace('/', '_', $retstrs);
        return $runtokey.$retstrs;
    } else {
        if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$egiskeys), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    }
}